<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\model;

class ArticleList extends \app\BaseModel
{
    protected $name = "massage_article_list";
    protected $append = ["field"];
    public function getFieldAttr($value, $data)
    {
        if(!empty($data["id"])) {
            $connect_model = new ArticleConnect();
            $dis = ["a.article_id" => $data["id"], "b.status" => 1];
            $list = $connect_model->alias("a")->join("massage_article_form_field b", "a.field_id = b.id")->where($dis)->field("a.*,b.title,b.field_type,b.is_required")->group("b.id")->order("b.top desc,a.id desc")->select()->toArray();
            return $list;
        }
    }
    public function getFieldTitle($id)
    {
        $dis = ["a.article_id" => $id, "b.status" => 1];
        $connect_model = new ArticleConnect();
        $list = $connect_model->alias("a")->join("massage_article_form_field b", "a.field_id = b.id")->where($dis)->field("a.*,b.title,b.field_type")->group("b.id")->order("b.top desc,b.id desc")->select()->toArray();
        return $list;
    }
    public function dataAdd($data)
    {
        $data["create_time"] = time();
        if(isset($data["field"])) {
            $field = $data["field"];
            unset($data["field"]);
        }
        $res = $this->insert($data);
        $id = $this->getLastInsID();
        if(!empty($field)) {
            $this->updateSome($id, $field, $data["uniacid"]);
        }
        return $res;
    }
    public function updateSome($id, $field, $uniacid)
    {
        $connect_model = new ArticleConnect();
        $connect_model->where(["article_id" => $id])->delete();
        foreach ($field as $k => $value) {
            $insert[$k] = ["uniacid" => $uniacid, "article_id" => $id, "field_id" => $value];
        }
        $connect_model->saveAll($insert);
        return true;
    }
    public function dataUpdate($dis, $data)
    {
        if(isset($data["field"])) {
            $field = $data["field"];
            unset($data["field"]);
        }
        $res = $this->where($dis)->update($data);
        if(!empty($field)) {
            $this->updateSome($dis["id"], $field, $data["uniacid"]);
        }
        return $res;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("top desc,id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
}

?>